package menu_api

import (
	"benkendSystem/database"
	"benkendSystem/models"
	"github.com/gin-gonic/gin"
	"net/http"
)

// UpdateMenu 跟新菜单信息
func UpdateMenu(c *gin.Context) {
	in := new(UpdateMenuRequest)
	err := c.ShouldBindJSON(in)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数错误",
		})
		return
	}
	if in.ID == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数异常",
		})
		return
	}
	err = database.DB.Model(new(models.Menu)).Where("id = ?", in.ID).Updates(map[string]any{
		"name":           in.Name,
		"parent_id":      in.ParentId,
		"order":          in.Order,
		"icon":           in.Icon,
		"level":          in.Level,
		"path":           in.Path,
		"component_name": in.ComponentName,
	}).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "系统错误",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "操作成功",
	})
}
